<?php
	####################################################
	# Nlog 콘솔 로그,                                  #
	####################################################
	# $addr디렉터리의 CHMOD는 0777 형태로 존재 하여야 한다. window에서는 별 상관 없음.

	class Nlog {
		private static $addr = NLOG;


		# 쿠키모드가 1일 경우 파일에 쓴다.
		#	example)
		#		$v = "값";
		#		Nlog::puts("키>>", $v);
		#			출력>> 키값
		#		Nlog::puts($v);
		#			출력>> 값
		public static function puts($arg1='', $arg2 ='') {

			if($arg2==''){
				//$message = $arg1;
				if(defined('APPMODE') && APPMODE == "dev"){
				//if(Cookier::get('appmode', 1)){
					Nlog::put('Nlog' , $arg1);
				} else {
					Nlog::rec('Nlog' , $arg1);
				}
			}else{
				//$message = $arg2;
				if(defined('APPMODE') && APPMODE == "dev"){
					Nlog::put($arg1, $arg2);
				} else {
					Nlog::rec($arg1, $arg2);
				}
			}

			//echo __FILE__;

		}


		# 단일로 사용하기 위해 존재
		# 화면에 xmp태그로 출력.
		public static function xmp($message, $look_and_feel = 'console') {
			//echo $message;
			if(is_array($message)){
				$message = print_r($message, true);
			}

			if($look_and_feel == 'console'){
				$out = sprintf("<xmp style=\"color:white;background-color:black;   text-align:left; width:100%%;display:block;font:normal 16px/16px 'Fixedsys Excelsior 3.01', 'Bitstream Vera Sans Mono';\">%s</xmp>", $message);
			} else if($look_and_feel == 'marine') {
				$out = sprintf("<xmp style=\"color:yellow;background-color:red;    text-align:left; width:100%%;display:block;font:normal 16px/16px 'Fixedsys Excelsior 3.01', 'Bitstream Vera Sans Mono';\">%s</xmp>", $message);
			} else if($look_and_feel == 'mac') {
				$out = sprintf("<xmp style=\"color:black;background-color:white;   text-align:left; width:100%%;display:block;font:normal 16px/16px 'Monaco', 'Bitstream Vera Sans Mono';\">%s</xmp>", $message);
			} else {
				$out = sprintf("<xmp>%s</xmp>", $message);
			}

			echo $out;
		}


		# 화면에 xmp태그로 출력.
		private static function put($keymessage, $message) {
			//echo $message;
			if((string)$message == ''){
				echo "Nlog::put >> type to 2 args value<br />";
				return;
			}
			if(is_array($message)){
				$message = print_r($message, true);
			}
			$out = sprintf("<xmp style=\"color:white;background-color:black;   text-align:left; width:100%%;display:block;font:normal 16px/16px 'Fixedsys Excelsior 3.01', 'Bitstream Vera Sans Mono';color:#00ff00\">%s>>\n%s</xmp>", $keymessage, $message);
			echo $out;
		}

		# 해당 파일에 쓴다.
		private static function rec($keymessage, $message) {
			//echo $message;
			if((string)$message == ''){
				//echo "Nlog::rec >> type to 2 args value<br />";
				return;
			}

			#changing array to string
			if(is_array($message)){
				$message = print_r($message, true);
			}

			#instance
			$stderr_instance = fopen($_SERVER["DOCUMENT_ROOT"].Nlog::$addr, "w+");
			fwrite($stderr_instance,$message."\n");
			fclose($stderr_instance);
		}

		# 콘솔에 보여준다.
		private static function trace_console() {
			$stderr_instance = fopen($_SERVER["DOCUMENT_ROOT"].Nlog::$addr, "r+");
			$out_instance = "<script>";
			while (($buffer = fgets($stderr_instance)) !== false) {
				$buffer_ln = str_replace("\n"," ",$buffer);
				$buffer_ln = str_replace("'"," ",$buffer_ln);
				$out_instance .= "try{ console.log('".$buffer_ln."') }catch(e){}\n";
			}
			$out_instance .= "</script>\n";
			echo $out_instance;
		}

		# 화면을 지우는 명령어. 브라우저에 객체는 존재 하나 동작하지 않음.
		//public static function cls() {
		//	$stderr_instance = fopen($_SERVER["DOCUMENT_ROOT"].Nlog::$addr, "r+");
		//	$out_instance = "<script>";
		//	$out_instance .= "try{ console.clear(); }catch(e){}\n";
		//	$out_instance .= "</script>\n";
		//	echo $out_instance;
		//}

		# 화면에 blockquote태그로 출력
		//public static function blq($array) {
		//	print 'Array<br>(<blockquote style="margin:0;">';
		//	foreach ($array as $key => $value) :
		//		print "[$key] => ";
		//		if (is_array($value)) :
		//			Nlog::blq($value);
		//		else :
		//			print "$value<br>";
		//		endif;
		//	endforeach;
		//	print "</blockquote>)<br>";
		//}
	}

?>